<?php
/**
 * Created by PhpStorm.
 * User: Acer
 * Date: 2022/2/2
 * Time: 21:23
 */

namespace app\admin\service;

use app\admin\models\AdminModel;
use app\admin\models\UserModel as UserModel;
use app\move\models\IdentityCardModel;


class LoginService {
    // 用户登录
    public function s_login($data){
        // 判断该用户是否是管理员
        $AdminModel =  new AdminModel();
        $IdentityCardModel = new IdentityCardModel();
        $identity_card = $IdentityCardModel->where('mobile',$data['mobile'])->find();
        $identity_card =obj_to_arr($identity_card);
        if(!$identity_card) json_err(-204,['该用户，']);
        if(!$identity_card['aid'])json_err(-210,['用户不是管理员！']);
        if(!$identity_card['uid'])json_err(-210,['该用户未实名认证，不符合管理员标准！']);
        // 判断是否是管理员
        $rootInfo = $AdminModel->where('rid',$identity_card['aid'])->find();
        $rootInfo =obj_to_arr($rootInfo);
        if((int)$rootInfo['state'] !== 1)json_err(-210,['该账号已禁用！']);

        if($rootInfo['password'] !== md5($data['password']))  json_err(-209,['密码，']);
        $identity_card['role'] = $rootInfo['role']; // 用户等级
        return $identity_card;
    }

    // 用户注册
    public function s_register($data){
        // 判断用户是否已注册
        $is_user = (new UserModel)->where('mobile',$data['mobile'])->value('mobile');
        if($is_user) json_err(-203,['该用户，']);
        // 进行注册
        $user_id = (new UserModel)->insertGetId([
            'username'  => $data['username'],
            'mobile'    => $data['mobile'],
            'password'  => md5($data['password']),
            'sex'       => $data['sex'],
            'province'  =>  $data['province'],
            'city'      =>  $data['city'],
            'area'      =>  $data['area'],
        ]);

        if(!$user_id) json_err(-205);
        $user_sign = getNonceStr(3).time();
        (new UserModel)->where('id',$user_id)->update([
                'user_sign' => $user_sign
        ]);
        return [
            'user_sign' => $user_sign,
            'user_id'   =>  $user_id
        ];

    }
    // 修改用户密码
    public function s_modifyPsd($psa,$uid){
        $is_user = (new UserModel)->where('id',$uid)->update([ 'password' => md5($psa) ]);
        return $is_user;
    }
    // 修改用户资料
    public function s_update_user($data,$uid){
        // 电话是否存在
        $user_id = (new UserModel)->where('id','<>',$uid)->where('mobile',$data['mobile'])->value('id');
        if($user_id) json_err(-203,['电话']);
        $user_id = (new UserModel)->where('id',$uid)->update([
            'username'  => $data['username'],
            'mobile'    => $data['mobile'],
            'sex'       => $data['sex'],
            'province'  =>  $data['province'],
            'city'      =>  $data['city'],
            'area'      =>  $data['area'],
        ]);

        if(!$user_id) json_err(-205);
        $user_sign = getNonceStr(3).time();
        (new UserModel)->where('id',$uid)->update([
            'user_sign' => $user_sign
        ]);
        return [
            'user_sign' => $user_sign,
            'province'  =>  $data['province'],
            'city'      =>  $data['city'],
            'area'      =>  $data['area'],
            'mobile'    => $data['mobile'],
            'username'  => $data['username'],
            'sex'       =>  $data['sex'],
        ];
    }
}